package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.ApplicationLeterDAO;
import com.chanyachting.emall.domain.Applicationleter;
import com.chanyachting.emall.domain.Educationhistory;
import com.chanyachting.emall.domain.Jobhistory;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.service.ApplicationLeterService;

public class ApplicationLeterServiceImpl implements ApplicationLeterService {
	
	
	private ApplicationLeterDAO aldao;
	
	
	public ApplicationLeterServiceImpl(){
		
		aldao=new ApplicationLeterDAO();
		
		
	}
	

	public boolean delApplicationleter(Applicationleter al) {
		
		al.setStatus("00");
		
		aldao.delete(al);
		
		return true;
	}
	

	//求职信息下架
	public boolean delReleaseApplicationleter(Applicationleter al) {
		
		al.setStatus("00");
		
		return aldao.updateApplicationleterStatus(al);
	}

	public List<Applicationleter> getAllApplicationleter() {
		
		
		List<Applicationleter> list=aldao.find("from Applicationleter");
		
		return list;
	}

	public List<Applicationleter> getApplicationleterByMember(Member member) {
		
		
		String args[]={String.valueOf(member.getId())};
		
		return aldao.find("from Applicationleter where memberId=?",args);
	}

	public List<Applicationleter> getNReleasedApplicationleter(Member member) {
		
		String args[]={String.valueOf(member.getId())};
		
		return aldao.find("from Applicationleter where memberId=? and status='00'",args);
	}

	public Applicationleter getOneApplicationLeter(Applicationleter al) {
		
		return (Applicationleter) aldao.load(Applicationleter.class, al.getId());
	}

	public List<Applicationleter> getReleasedApplicationleter(Member member) {
		
		String args[]={String.valueOf(member.getId())};
		
		return aldao.find("from Applicationleter where memberId=? and status='01'",args);
	}

	
	
	//刷新求职信息
	public void refreshApplicationleter(Applicationleter al) {
		
		aldao.refreshApplicationleter(al);

	}

	
	//上架求职信息
	public boolean releaseApplicationleter(Applicationleter al) {
		
		al.setStatus("01");
		
		return aldao.updateApplicationleterStatus(al);
	
	}

	public boolean updateApplicationleterInfo(Applicationleter al) {
		
		return aldao.updateApplicationleterInfo(al);
	}


	public boolean createApplicationleter(Applicationleter al) {
		
		aldao.save(al);
		
		return true;
	}


	public boolean addEducationhistory(Applicationleter al, Educationhistory eh) {
		
		
		return this.aldao.addEducationhistory(al, eh);
		
	}


	public boolean addJobHistory(Applicationleter al, Jobhistory jh) {
	
		
		return this.aldao.addJobHistory(al, jh);
	}


	public void delEducationhistory(Educationhistory eh) {
		
		
		this.aldao.delete(eh);
		
	}


	public void delJobHistory(Jobhistory jh) {
		
		this.aldao.delete(jh);
		
	}


	public boolean updateEducationhistory(Educationhistory eh) {
		
		return this.aldao.updateEducationhistory(eh);
	}


	public boolean updateJobHistory(Jobhistory jh) {
		
		return this.aldao.updateJobHistory(jh);
	}

	public List<Applicationleter> getApplicationleterByKeyword(String keyword) {
		return this.aldao.find("from Applicationleter where wantPostSummary like '" + keyword +"'");
	}

	public List<Applicationleter> getAllApplicationleterByPageBreak(int currentPage,int pageSize) {
		String sqlStr = "from Applicationleter where status = '01' order by releaseDate desc";
		Query q = this.aldao.createQuery(sqlStr);

		q.setFirstResult(currentPage * pageSize);
		q.setMaxResults(pageSize);
		
		return q.list();
	}
	
	public int getApplicationleterCount(String status) {
		String sqlStr = "select count(*) from Applicationleter where status = ?";
		
		return (Integer)this.aldao.find(sqlStr, new String[]{status}).get(0);
	}
}
